<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
          <!-- 用一个行列布局 -->
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>

          <!-- 树形图布局 -->
          <el-tree :data="data" >

            <template #default="{ data}">
                <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{ data.name}}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{data.manager}}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length===0" @click.native="hDele(data.id)">删除子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
            </template>

          </el-tree>

<!-- 弹层 -->
          <el-dialog
  :title="edit?'编辑部门':'添加部门'"
  :visible.sync="dialogVisible"
  width="35%"
  :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="resetForm" 
  >
  <!-- 子组件 -->
  <Child :id="usId" @apply="hApply" :check="check" :edit="edit" ref="refs"></Child>
  </el-dialog>
  
      </el-card>   
    </div>
  </div>
</template>




<script>
import { addDepartments,deleDepartments } from "@/api/Organization";
import { tranDataToTree} from "@/utils";
import Child from "@/views/departments/deptDialog";
// console.log(tranDataToTree);
  export default {
    components:{
        Child,
    },
    data() {
      return {
        dialogVisible:false,
        data: [],
        usId :'',
        edit:false,
        check:[]
      };
    },
    created(){
        this.formation()
    },
    methods: {
     
      // 重置
      resetForm(){
        this.$refs.refs.resetForm()
      },
      // 删除
      hDele(id){
         this.$confirm('是否确认删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
         try {
          await deleDepartments(id)
          this.formation()
         } catch (error) {
          console.log(error);
         }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      // 编辑
      hEdit(id){
        this.edit=true
        this.dialogVisible=true
        this.usId=id
     this.$nextTick(function () {
          this.$refs.refs.idLoadDepartments()
        });
        
      },
      // 渲染
      hApply(){
        this.dialogVisible=false,
        this.formation()
      },
      // 添加子组件
      hAdd(id){
        this.dialogVisible=true
        this.usId=id
        this.edit=false
        
      },
      // 查询企业的部门列表
     async  formation(){
        const res = await addDepartments()
        // console.log(res);
        res.data.depts.shift()
        this.data= tranDataToTree(res.data.depts) 
        this.check = res.data.depts.map((item)=>{
          return {id:item.id,pid:item.pid,code:item.code,name:item.name}
        })
      }
    }
  };
</script>